package org.erp.spu.controller;



import org.erp.common.domain.Result;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;

import org.erp.spu.entity.bo.SpuCategoryBo;
import org.erp.spu.entity.dto.SpuCategoryDto;
import org.erp.spu.entity.vo.SpuCategoryVo;
import org.erp.spu.service.SpuCategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.erp.common.domain.Result.success;

/**
 * 分类表(SpuCategory)表控制层
 *
 * @since 2025-11-05 10:10:32
 */
@RestController
@RequestMapping("/category")
public class SpuCategoryController  {
    /**
     * 服务对象
     */
    @Resource
    private SpuCategoryService spuCategoryService;

    /**
     * 分页查询所有数据
     *
     * @param spuCategoryBo 查询实体
     * @return 所有数据
     */
    @GetMapping("getListByPage")
    public Result<DataPageResp<SpuCategoryVo>> selectAll(SpuCategoryBo spuCategoryBo) {


        PageQueryModel<SpuCategoryVo> queryModel = spuCategoryService.getListByPage(spuCategoryBo);
        return success(DataPageResp.of(queryModel.getTotal(), queryModel.getDataList()));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public Result<SpuCategoryVo> selectOne(@PathVariable Long id) {
        return Result.success(spuCategoryService.getSpuCategoryVoById(id));
    }

    /**
     * 获取所有数据
     *
     *
     * @return
     */
    @GetMapping("getList")
    public Result<List<SpuCategoryVo>> getList(SpuCategoryBo spuCategoryBo) {
        return Result.success(spuCategoryService.getList(spuCategoryBo));
    }

    /**
     * 新增数据
     *
     * @param spuCategoryDto 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    public Result<Boolean> insert(@RequestBody SpuCategoryDto spuCategoryDto) {
           boolean result = spuCategoryService.addSpuCategory(spuCategoryDto);
        return  result ? Result.success("新增成功") : Result.error("新增失败");
    }

    /**
     * 修改数据
     *
     * @param spuCategoryDto 实体对象
     * @return 修改结果
     */
    @PostMapping("/upd")
    public Result<Boolean> update(@RequestBody SpuCategoryDto spuCategoryDto) {
        boolean result = spuCategoryService.updateSpuCategory(spuCategoryDto);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("del")
    public Result<Boolean> delete(@RequestParam("idList") List<Long> idList) {
        boolean result =spuCategoryService.removeByIds(idList);
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }


}

